package com.huweilong.util;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author 是阿秋啊
 * @date 2022/04/09 10:22
 **/
public class DBUtil {
    // MysqlDataSource对象只需要一份所以，一个static就够了
    private static final DataSource dataSource;
    static {
        MysqlDataSource db = new MysqlDataSource();
       // db.setUrl("jdbc:mysql://localhost:3306:useSSl = false&characterEncoding = utf-8&severTimezone=Asia/Shanghai");
        db.setServerName("localhost");
        db.setPort(3306);
        db.setUser("root");
        db.setPassword("123456789");
        db.setDatabaseName("library");
        db.setUseSSL(false);
        db.setCharacterEncoding("utf-8");
        db.setServerTimezone("Asia/Shanghai");
        // 等同于上边一句话
        dataSource = db;
    }
    public static Connection connection() throws SQLException {
        return dataSource.getConnection();
    }
}
